<template>
  <div class="tabbar-home-wapper">
    <van-nav-bar
      title="物资列表"
      left-text="返回"
      left-arrow
      fixed
      @click-left="onClickLeft"
      @click-right="onClickRight"
    >
      <van-icon name="scan" slot="right" />
      <van-icon name="search" slot="left" />
    </van-nav-bar>
    <div class="tabbar-content">
      <van-pull-refresh
        v-model="isLoading"
        success-text="刷新成功"
        @refresh="onRefresh"
      >
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <van-cell
            value="物资详情"
            label=""
            is-link
            @click="showInfo(item.itemid)"
            v-for="(item, index) in list"
            :key="index"
          >
            <!-- 使用 title 插槽来自定义标题 -->
            <template slot="title">
              <span class="custom-title">{{ item.description }}</span>
              <!--<van-tag type="primary" style="margin-right: 5px;">{{item.itemid}}</van-tag>-->
              <!--<van-tag type="success">{{item.itemweight| filterType}}</van-tag>-->
            </template>
            <template slot="label">
              <van-tag type="primary" style="margin-right: 5px;">{{
                item.itemid
              }}</van-tag>
              <!-- <van-tag type="success">{{
                item.itemweight | filterType
              }}</van-tag> -->
            </template>
          </van-cell>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script>
import { getAll } from "@/api/goods.js";
import { Toast } from "vant";
export default {
  name: "TabbarHome",
  data() {
    return {
      list: [],
      isLoading: false,
      loading: false,
      finished: false,
      pageInfo: {
        page: 0,
        pageSize: 20
      }
    };
  },
  methods: {
    onClickLeft() {
      console.log("onClickLeft");
      this.$router.push({
        path: "/search"
      });
    },
    onClickRight() {
      console.log("onClickRight");
      this.$router.push({
        path: "/scancode",
        query: { params: "id" }
      });
    },
    showInfo(id) {
      this.$router.push({
        path: "/home/info",
        query: { id: id }
      });
    },
    getGoodsList(flag) {
      var that = this;
      getAll(this.pageInfo).then(res => {
        that.loading = false;
        if ((res.data != null) && (res.data.length > 0)) {
          this.list = this.list.concat(res.data);
        } else {
          that.finished = true;
        }
        if (flag) {
          Toast("刷新成功");
          this.isLoading = false;
        }
      });
    },
    onLoad() {
      this.pageInfo.page += 1;
      this.getGoodsList();
      // 异步更新数据
      /* setTimeout(() => {



                    for (let i = 0; i < 10; i++) {
                        this.list.push(this.list.length + 1);
                    }
                    // 加载状态结束
                    this.loading = false;

                    // 数据全部加载完成
                    if (this.list.length >= 30) {
                        this.finished = true;
                    }
                }, 2000); */
    },
    onRefresh() {
      this.pageInfo.page = 1;
      this.list = [];
      this.getGoodsList(true);
    }
  },
  filters: {
    filterType: function(val) {
      switch (val) {
        case "STK":
          return "常备库存项目";
          break;
        case "NS":
          return "非常备库存项目";
          break;
        case "SP":
          return "特别订购项目";
          break;
        default:
          return "未知类型";
      }
    }
  },
  created() {
    // console.log(this.$route.query.id)
    this.pageInfo.itemid = this.$route.query.itemid;
    this.pageInfo.description = this.$route.query.description;
    this.pageInfo.refrence = this.$route.query.refrence;
    this.pageInfo.spec = this.$route.query.spec;
    this.pageInfo.subsystemid = this.$route.query.subsystemid;
  
  }
};
</script>

<style scoped lang="scss">
.tabbar-home-wapper {
  position: relative;

  /*height: calc(100% - 50px);*/
  .tabbar-content {
    position: relative;
    top: 46px;
    height: calc(100% - 50px);
    margin-bottom: 10px;
    overflow-y: auto;
  }

  .custom-title {
    padding-right: 5px;
    font-weight: bold;
  }
}
</style>
